/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.blogcomment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.modular.blogcomment.entity.BlogComment;
import vip.xiaonuo.modular.blogcomment.enums.BlogCommentExceptionEnum;
import vip.xiaonuo.modular.blogcomment.mapper.BlogCommentMapper;
import vip.xiaonuo.modular.blogcomment.param.BlogCommentParam;
import vip.xiaonuo.modular.blogcomment.service.BlogCommentService;

import java.util.List;

/**
 * 评论service接口实现类
 *
 * @author 刘文理
 * @date 2021-08-18 15:06:33
 */
@Service
public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogComment> implements BlogCommentService {


    @Override
    public PageResult<BlogComment> page(BlogCommentParam blogCommentParam, Boolean isParent) {

        QueryWrapper<BlogComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogComment::getIsDeleted, 0);

        if (ObjectUtil.isNotNull(blogCommentParam)) {
            if (ObjectUtil.isNotEmpty(isParent) && isParent) {
                // 该字段为空 则为top
                queryWrapper.lambda().isNull(BlogComment::getParentId);
            }
            // 根据关联的blog主键 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getBlogId())) {
                queryWrapper.lambda().eq
                        (BlogComment::getBlogId, blogCommentParam.getBlogId());
            }
            // 根据评论者名称 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getCommentator())) {
                queryWrapper.lambda().eq
                        (BlogComment::getCommentator, blogCommentParam.getCommentator());
            }
            // 根据评论人的邮箱 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getEmail())) {
                queryWrapper.lambda().eq
                        (BlogComment::getEmail, blogCommentParam.getEmail());
            }
            // 根据网址 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getWebsiteUrl())) {
                queryWrapper.lambda().eq
                        (BlogComment::getWebsiteUrl, blogCommentParam.getWebsiteUrl());
            }
            // 根据评论内容 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getCommentBody())) {
                queryWrapper.lambda().eq
                        (BlogComment::getCommentBody, blogCommentParam.getCommentBody());
            }
            // 根据评论时的ip地址 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getCommentatorIp())) {
                queryWrapper.lambda().eq
                        (BlogComment::getCommentatorIp, blogCommentParam.getCommentatorIp());
            }
            // 根据回复父ID 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getParentId())) {
                queryWrapper.lambda().eq
                        (BlogComment::getParentId, blogCommentParam.getParentId());
            }
            // 根据是否审核通过 0-未审核 1-审核通过 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getCommentStatus())) {
                queryWrapper.lambda().eq
                        (BlogComment::getCommentStatus, blogCommentParam.getCommentStatus());
            }
            // 根据是否删除 0-未删除 1-已删除 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getIsDeleted())) {
                queryWrapper.lambda().eq
                        (BlogComment::getIsDeleted, blogCommentParam.getIsDeleted());
            }
            queryWrapper.lambda().orderByDesc(BlogComment::getCreateTime);
            queryWrapper.lambda().select(BlogComment::getCommentId, BlogComment::getCommentator, BlogComment::getCommentBody,
                    BlogComment::getParentId, BlogComment::getCreateTime, BlogComment::getBlogId, BlogComment::getCommentStatus,
                    BlogComment::getWebsiteUrl);
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public PageResult<BlogComment> childrenPage(BlogCommentParam blogCommentParam, Boolean isParent) {

        QueryWrapper<BlogComment> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(blogCommentParam)) {
            if (ObjectUtil.isNotEmpty(isParent) && isParent) {
                // 该字段为空 则为top
                queryWrapper.lambda().isNull(BlogComment::getParentId);
            }
            // 根据关联的blog主键 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getBlogId())) {
                queryWrapper.lambda().eq
                        (BlogComment::getBlogId, blogCommentParam.getBlogId());
            }
            // 根据回复父ID 查询
            if (ObjectUtil.isNotEmpty(blogCommentParam.getParentId())) {
                queryWrapper.lambda().eq
                        (BlogComment::getParentId, blogCommentParam.getParentId());
            }
            // 根据是否审核通过 0-未审核 1-审核通过 查询
            // 根据是否删除 0-未删除 1-已删除 查询
            queryWrapper.lambda().eq
                    (BlogComment::getCommentStatus, 1);
            queryWrapper.lambda().eq
                    (BlogComment::getIsDeleted, 0);
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<BlogComment> list(BlogCommentParam blogCommentParam) {
        return this.list();
    }

    @Override
    public void add(BlogCommentParam blogCommentParam) {
        BlogComment blogComment = new BlogComment();
        BeanUtil.copyProperties(blogCommentParam, blogComment);
        blogComment.setCommentBody(blogComment.getCommentBody());
        this.save(blogComment);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(BlogCommentParam blogCommentParam) {
        this.removeById(blogCommentParam.getCommentId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean fakeDelete(BlogCommentParam blogCommentParam) {
        BlogComment comment = new BlogComment();
        comment.setCommentId(blogCommentParam.getCommentId()).setIsDeleted(1);
        return this.updateById(comment);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BlogCommentParam blogCommentParam) {
        BlogComment blogComment = this.queryBlogComment(blogCommentParam);
        BeanUtil.copyProperties(blogCommentParam, blogComment);
        this.updateById(blogComment);
    }

    @Override
    public BlogComment detail(BlogCommentParam blogCommentParam) {
        return this.queryBlogComment(blogCommentParam);
    }

    /**
     * 获取评论
     *
     * @author 刘文理
     * @date 2021-08-18 15:06:33
     */
    private BlogComment queryBlogComment(BlogCommentParam blogCommentParam) {
        BlogComment blogComment = this.getById(blogCommentParam.getCommentId());
        if (ObjectUtil.isNull(blogComment)) {
            throw new ServiceException(BlogCommentExceptionEnum.NOT_EXIST);
        }
        return blogComment;
    }

    @Override
    public PageResult<BlogComment> getCommentTree(BlogCommentParam param) {
        PageResult<BlogComment> page = new PageResult<>();
        Integer offset = (param.getPage()-1)*param.getSize();
        param.setOffset(offset);

        Integer total = baseMapper.total(param.getBlogId().toString());

        List<BlogComment> tree = baseMapper.getCommentTree(param);

        page.setTotalRows(total);
        page.setRows(tree);

        return page;
    }
}